package com.udaos.modules.train.controllers;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.customer.services.UdCustomerService;
import com.udaos.modules.product.services.UdProductTypeService;
import com.udaos.modules.project.services.UdProjectService;
import com.udaos.modules.region.services.UdRegionService;
import com.udaos.modules.train.services.UdTrainlumpOrderService;
import com.udaos.modules.train.services.UdTrainlumpProductService;


/**
 * @author mutaotao
 * @time 2017-03-10 12:58:20
 * 
 */
@IocBean
@At("/platform/train/lumpOrder")
@Filters({ @By(type = PrivateFilter.class)})
public class UdTrainlumpOrderController {
	@Inject
	private UdTrainlumpOrderService trainlumpOrderService;
	@Inject
	private UdCustomerService customerService;
	@Inject
	private UdRegionService regionService;
	@Inject
	private UdProjectService projectService;
	@Inject
	private UdProductTypeService productTypeService;
	@Inject
	private UdTrainlumpProductService trainlumpProductService;

	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/train/lumporder/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	/**
	 * 加载数据
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("state", "=", 1).or("state", "=", 2);
		cnd.and("delFlag", "=", "0");
		//获取主体
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		String company_id=user.getUnit().getCompany_id();
		cnd.and("company_id", "=", company_id);
		return trainlumpOrderService.data(length, start, draw, order, columns, cnd, null);
	}
	
	@At
	@Ok("json")
	public String getCustomer(String id){
		return customerService.fetch(id).getMobile();
	}
	@At
	@Ok("json")
	public String getRegion(String id){
		return regionService.fetch(id).getName();
	}
	@At
	@Ok("json")
	public String getProject(String id){
		return projectService.fetch(id).getName();
	}
	@At
	@Ok("json")
	public String getProductType(String id){
		return productTypeService.fetch(id).getName();
	}
	@At
	@Ok("json")
	public String getProduct(String id){
		return trainlumpProductService.fetch(id).getName();
	}
}